$(document).ready(function() {
    var websiteUrl = $('#websiteUrl').val();
    $('#silo-name').val('add silo...');
    $('#silo-name').css('color', '#7c7c7c');
    $('#silo-name').click(function() {
	if($(this).val() == 'add silo...') {
	    $(this).val('');
	}
    })

    $('#addbutton').click(function() {
	var siloName = $('#silo-name').val();
	if(siloName == 'add silo...') {
	    $('#silo-name').val('');
	    siloName = '';
	}
	//saving silo
	$.ajax({
	    type : 'post',
	    url:websiteUrl+'sys/backend_prsculpting/savesilo/',
	    data: ({
		name: siloName
	    }),
	    success: function(response) {
		result = eval("(" + response + ")");
		if(isNaN(result.error)) {
		    switch (result.error) {
			case 'empty':
			    $('#prs-errors').show();
			    $('#prs-empty').show();
			break;
			case 'toshort':
			    $('#prs-errors').show();
			    $('#prs-lenght-short').show();
			break;
			case 'tolong':
			    $('#prs-errors').show();
			    $('#prs-lenght').show();
			break;
			case 'exists':
			    $('#prs-errors').show();
			    $('#prs-exists').show();
			break;
			case 'forbid':
			    $('#prs-errors').show();
			    $('#prs-forbid').show();
			break;
		    }
		}
		else {
		    $('#prs-success').show();
		    $('select.silo').append('<option value="'+ result.data +'">'+ siloName +'</option>');
		    var silosHtml = makeSilosHtml(result.additional);
		    $('#silos-list').html(silosHtml);
		}
	    }
	});
    });

    $("#chkall").click(function() {
	var checkedStatus = $(this).attr('checked');
	$("input").attr('checked', checkedStatus);
    });
});

function rmSilo(id, name) {
    var websiteUrl = $('#websiteUrl').val();
    $.ajax({
	type : 'post',
	url:websiteUrl+'sys/backend_prsculpting/removesilo/',
	data: ({
	    siloId: id
	}),
	success: function(response) {
	    var evaledResponse = eval("(" + response + ")");
	    if(evaledResponse.error == 0) {
		var silos = evaledResponse.data;
		var silosHtml = makeSilosHtml(silos);
		$('#silos-list').html(silosHtml);
		$('#silorm-msg').html(evaledResponse.msg);
		$('#silorm-msg').addClass('success');
		$('#silorm-msg').show();
		$('.silo option').remove(':contains("' + name + '")');
	    }
	//alert(evaledResponse.msg);
	}
    });
}

function makeSilosHtml(silos) {
    var html = '';
    if(silos == '') {
	html += 'No silos yet';
    }
    else {
	html += '<table style="width:265px;"><tr><th colspan="2" style="text-align:center;">Remove silos</th></tr>';

	for(var i=0; i<=silos.length-1; i++) {
	    html += '<tr><td>' + silos[i].name + '</td><td><a href="javascript:;" onclick="rmSilo('+ silos[i].id +', \'' + silos[i].name + '\')">delete</a></td></tr>';
	}
	html += '</table>';
    }
    html += '<div style="position:absolute;bottom:10px;text-align: center;width:95%"><a href="javascript:;" onclick="$(\'#silos-list\').hide(); $(\'#how-to-remove\').show();">Close</a></div>'
    return html;
}

function refreshScreen() {
    var websiteUrl = $('#websiteUrl').val();
    $('.prsculpting').load(websiteUrl+'sys/backend_prsculpting/index/');
}

function hideMessages() {
    $('#prs-success').hide();
    $('#prs-errors').hide();
    $('#prs-exists').hide();
    $('#prs-empty').hide();
    $('#prs-lenght').hide();
    $('#prs-lenght-short').hide();
    $("#silo-name").val('');
}

function siloCotegory(el) {
    var websiteUrl = $('#websiteUrl').val();
    var siloNameClass = el.id;
    siloName = siloNameClass.replace('-', ' ');
    var isChecked = $(el).attr('checked');
    if(isChecked) {
	$.ajax({
	    type : 'post',
	    url:websiteUrl+'sys/backend_prsculpting/savesilo/',
	    data: ({
		name: siloName,
		asCat: isChecked
	    }),
	    success: function(response) {
                result = eval("(" + response + ")");
                
                if(!result.error) {
		    $('select.silo').append('<option value="'+ response +'">'+ siloName +'</option>');
		}

                var selects = $.find('select.silo');
		$.each(selects, function() {
		    if($(this).hasClass(siloNameClass)) {
			$(this).val(siloName);
		    }
		});
	    }
	})
    }
    else {
	$.ajax({
	    type : 'post',
	    url:websiteUrl+'sys/backend_prsculpting/removeascat/',
	    data: ({
		name: siloName
	    }),
	    success: function(response) {
		var selects = $.find('select.silo');
		$.each(selects, function() {
		    if($(this).hasClass(siloNameClass)) {
			$(this).val('none');
		    }
		});
	    }
	})
    }
}